package com.jwthhealth_pub.common.base;

import android.graphics.PixelFormat;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroupOverlay;
import android.view.WindowManager;
import android.widget.ProgressBar;

/**
 * contain
 *  show & hide progressbar
 * Created by liuzhen on 2017/12/4.
 */

public abstract class BasetFragment extends Fragment {
    private ProgressBar mProgressbar;
    public boolean isProgressBarShow = false;

    protected abstract void title();

    public void showLoadProgresbar() {

        isProgressBarShow = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //api 19
            ViewGroup parent = (ViewGroup) getActivity().getWindow().getDecorView().getRootView();
            ViewGroupOverlay mProgressbarOverlay = parent.getOverlay();

            DisplayMetrics displaymetrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int screenWidth = displaymetrics.widthPixels;
            int screenHeight = displaymetrics.heightPixels;

            View mProegressbarView = new ProgressBar(getActivity());
            mProegressbarView.layout(screenWidth / 2 - 50, screenHeight / 2 - 50, screenWidth / 2 + 50, screenHeight / 2 + 50);
            mProgressbarOverlay.add(mProegressbarView);
        } else {
            // < api 19
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int screenWidth = displaymetrics.widthPixels;
            int screenHeight = displaymetrics.heightPixels;

            WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams();
            mWindowParams.gravity = Gravity.TOP;
            mWindowParams.x = screenWidth / 2 - 50;
            mWindowParams.y = screenHeight / 2 - 50;
            mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
            mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
//                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
//                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
            mWindowParams.format = PixelFormat.TRANSLUCENT;
            mWindowParams.windowAnimations = 0;
            mWindowParams.gravity = Gravity.TOP | Gravity.START;

            mProgressbar = new ProgressBar(getActivity());
//        int backGroundColor = getResources().getColor(R.color.primarycolor);
//        v.setBackgroundColor(backGroundColor);
//        v.setImageBitmap(bm);

//        mWindowManager = (WindowManager)getSystemService("window");
            WindowManager windowManager = getActivity().getWindowManager();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
                windowManager.addView(mProgressbar, mWindowParams);
        }
    }

    public void missLoadProgressbar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getActivity().getWindow().getDecorView().getRootView().getOverlay().clear();
        } else {
            WindowManager windowManager = getActivity().getWindowManager();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
                windowManager.removeView(mProgressbar);
        }

        isProgressBarShow = false;
    }

    @Override
    public void onStart() {
        super.onStart();
        title();
    }
}
